/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package am.aua.renju.bussinesslayer.gamelogic;

/**
 *
 * @author ara
 */
public class GameLogic {

    private PlayData lastPlay;

    public static enum GameLogicState {

        NONE, WHITE_WON, BLACK_WON, DRAW,
    }
    
    private GameLogicState gameLogicState;
    private BoardPositionState[][] board;    
    private boolean isWhite;

    public GameLogic() {
        board = new BoardPositionState[15][15];
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                board[i][j] = BoardPositionState.EMPTY;
            }
        }
    }

    public PlayData getLastMove() {
        return lastPlay;
    }

    public GameLogicState playBlack(int x, int y) {
        if (!isWhite) {
            if (board[x][y] == BoardPositionState.EMPTY) {
                board[x][y] = BoardPositionState.BLACK;
                isWhite = true;
                if (Checker.isVictory(board, false, x, y)) {
                    lastPlay = new PlayData(GameLogicState.BLACK_WON, x, y, false);
                    this.gameLogicState=GameLogicState.BLACK_WON;
                    return GameLogicState.BLACK_WON;
                }
                if (Checker.isDraw(board)) {
                    lastPlay = new PlayData(GameLogicState.DRAW, x, y, false);
                    this.gameLogicState=GameLogicState.DRAW;
                    return GameLogicState.DRAW;
                }
                lastPlay = new PlayData(GameLogicState.NONE, x, y, false);
                this.gameLogicState=GameLogicState.NONE;
                return GameLogicState.NONE;
            }
        }
        return null;
    }

    public GameLogicState playWhite(int x, int y) {
        if (isWhite) {
            if (board[x][y] == BoardPositionState.EMPTY) {
                board[x][y] = BoardPositionState.WHITE;
                isWhite = false;
                if (Checker.isVictory(board, true, x, y)) {
                    lastPlay = new PlayData(GameLogicState.WHITE_WON, x, y, true);
                    this.gameLogicState=GameLogicState.WHITE_WON;
                    return GameLogicState.WHITE_WON;
                }
                if (Checker.isDraw(board)) {
                    lastPlay = new PlayData(GameLogicState.DRAW, x, y, true);
                    this.gameLogicState=GameLogicState.DRAW;
                    return GameLogicState.DRAW;
                }
                lastPlay = new PlayData(GameLogicState.NONE, x, y, true);
                this.gameLogicState=GameLogicState.NONE;
                return GameLogicState.NONE;
            }
        }
        return null;
    }

    public BoardPositionState[][] getBoard() {
        return board;
    }
    
    public GameLogicState getGameLogicState(){
        return gameLogicState;
    }
}
